﻿@model IEnumerable<RMS.Models.Product>
@using RMS.Models;

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@if (Request.IsAuthenticated && (Roles.GetRolesForUser().Contains("Company")))
{
    <p>
        [ @Html.ActionLink("Create ProductGroup", "CreateProductGroup") ]
        [ @Html.ActionLink("Create Product", "Create") ]
        [ @Html.ActionLink("Create General Problem", "Create", "Problem", new { companyId = (int)ViewData["companyId"] }, null) ]
    </p>
}
<table>
@{
    try{
        foreach (var producrGroup in (ViewData["productGroups"] as IEnumerable<ProductGroup>)) 
        {
            <tr>
                <th colspan="3">
                    @producrGroup.Name
                </th>
            </tr>
            if(Model.Any(x=>x.ProductGroupId == producrGroup.Id))
            {
                foreach(var product in Model.Where(x=>x.ProductGroupId == producrGroup.Id))
                    {
                        <text>
                        <tr>
                            <td>
                                @if(Request.IsAuthenticated)
                                {
                                    @Html.ActionLink("Details", "Details", new { id=product.Id })
                                }
                            </td>
                            <td>
                                @product.Name
                            </td>
                            <td>
                                @if(Request.IsAuthenticated)
                                {
                                    @Html.ActionLink("New Problem", "Create", "Problem", new { productId = product.Id,companyId = ViewData["companyId"].ToString() }, null)
                                }
                            </td>
                        </tr>
                        </text>
                    }
            }
            else
            {
                <text>
                    <tr>
                        <td colspan="3">
                            No product in this group
                        </td>
                    </tr>
                </text>
            }
        }
    }
    catch{}
}
</table>

